<?php

namespace App\Http\Controllers;

use App\Parts;
use App\Batch;
use App\PartsBatch;
use App\Storehouse;
use App\PartsInventory;
use Illuminate\Http\Request;

class BatchController extends Controller
{
    public function index()
    {
        $list   = Batch::orderby('code','asc')->paginate(10);
        $s_info = Storehouse::pluck('name','id');
        return view('batch.index', compact('list','s_info'));
    }

    public function store(Request $request, $id = '')
    {
        $data = $request->except('_token');
        $data1=[
            'code'=>$data['code'],
        ];
        $data2=[
            'storehouse_id'=>$data['store_id'],
        ];
        try {
            if ($id) {
                Batch::whereId($id)->update($data1);
                PartsInventory::where('partsbatch_id',$id)->update($data2);
            } else {
                $batch=Batch::create($data1);
                $data3=[
                    'storehouse_id'=>$data['store_id'],
                    'partsbatch_id'=>$batch->id,
                ];
                PartsInventory::create($data3);
            }
        } catch (\Exception $e) {

            return redirect()->back()->with('rs', ['flag' => 'error', 'msg' => '编辑失败']);
        }
        return redirect('batch')->with('rs', ['flag' => 'success', 'msg' => '编辑成功']);
    }

    public function getInfo($id)
    {
        try {
            $oneBatch=Batch::where('id',$id)->first();
            $store_id=PartsInventory::where('partsbatch_id',$id)->first()->storehouse_id;
            return response()->json(['rs' => 'true', 'data' => $oneBatch,'store_id' => $store_id]);
        } catch (\Exception $e) {;
            return response()->json(['rs' => 'false', 'msg' => '错误的信息']);
        }
    }

    public function doDelete(Request $request)
    {
        try {
            $id = $request->get('_id');
            // 如果不存在ID,则返回错误
            if (!$id) {
                return response()->json(['rs' => 'false', 'msg' => '错误信息']);
            }

            $ids = explode(',', $id);

            Batch::whereIn('id', $ids)->delete();
            PartsInventory::whereIn('partsbatch_id',$ids)->delete();

            return response()->json(['rs' => 'true', 'msg' => '操作成功']);
        } catch (\Exception $e) {;
            return response()->json(['rs' => 'false', 'msg' => '请刷新重新尝试']);
        }
    }
    /**
     * 跳转到添加配件页面
     * @author wxh
     */
    public function addparts($batch_id)
    {
        $list   = Parts::pluck('name','id');
        $parts  = PartsBatch::where('batch_id',$batch_id)->get();
        return view('batch.addparts',compact('list','batch_id','parts'));
    }

    public function setparts(Request $request)
    {
        $data=$request->except('_token');
        PartsBatch::where('batch_id',$data['batch_id'])->delete();
        for ($i=0;$i<count($data['part_id']);$i++){
            $data1=[
                'batch_id'=>$data['batch_id'],
                'parts_id' => $data['part_id'][$i],
                'number' => $data['num'][$i],
            ];
            PartsBatch::create($data1);
        }
        return redirect('batch')->with('rs', ['flag' => 'success', 'msg' => '编辑成功']);
    }

    public function deleteparts(Request $request)
    {
        try {
            $id = $request->get('_id');
            // 如果不存在ID,则返回错误
            if (!$id) {
                return response()->json(['rs' => 'false', 'msg' => '错误信息']);
            }

            $ids = explode(',', $id);
            PartsBatch::whereIn('id', $ids)->delete();
            return response()->json(['rs' => 'true', 'msg' => '操作成功']);
        } catch (\Exception $e) {;
            return response()->json(['rs' => 'false', 'msg' => '请刷新重新尝试']);
        }
    }
}
